package com.example.petstore.controller;

import com.example.petstore.common.CommonResponse;
import com.example.petstore.domain.Category;
import com.example.petstore.domain.Item;
import com.example.petstore.domain.Product;
import com.example.petstore.service.CategoryService;
import com.example.petstore.service.ItemService;
import com.example.petstore.service.LineItemService;
import com.example.petstore.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
//import java.util.Map;

@Controller
@SessionAttributes({"loginAdmin"})
public class ItemController {
    @Autowired
    private ItemService itemService;
    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private LineItemService lineItemService;
    @GetMapping("/item")/*test ok*/
    public String showItem(String itemId, ModelMap model){
        CommonResponse commonResponse = itemService.getItemByItemid(itemId);
        if(commonResponse.isSuccess()){
            Item item = (Item) commonResponse.getData();
            itemService.addViews(item.getItemid());
            model.addAttribute("item", item);
//            System.out.println(commonResponse.getData());
//            到item详情页面
            return "catalog/item";
        }
//        到首页
        return "catalog/main";
    }
    @ResponseBody
    @GetMapping("/admin/product")/*test ok*/
    public CommonResponse productAdd(Model model) {
        model.addAttribute("categoryList",categoryService.getCategoryList());
        return categoryService.getCategoryList();

    }
    @ResponseBody
    @GetMapping("/admin/item")/*test ok*/
    public CommonResponse itemAdd(Model model) {
        model.addAttribute("categoryList",categoryService.getCategoryList());
        model.addAttribute("productList",productService.getProductList());
        model.addAttribute("productCategoryList",productService.getProductIdsByCategory());
        System.out.println(productService.getProductIdsByCategory());
        return productService.getProductIdsByCategory();

    }
    @ResponseBody
    @GetMapping("/admin/showProduct")//Product展示页面
    public CommonResponse showProduct(Model model) {
        model.addAttribute("productList",productService.getProductList());
        System.out.println(productService.getProductList());
        return productService.getProductList();

    }
    @ResponseBody
    @GetMapping("/admin/showCategory")//Category展示页面
    public CommonResponse showCategory(Model model) {
        model.addAttribute("categoryList",categoryService.getCategoryList());
        System.out.println(categoryService.getCategoryList());
        return categoryService.getCategoryList();

    }

    @ResponseBody
    @PostMapping("/admin/removeItem")/*test ok*/
    public CommonResponse removeItem(@RequestParam("itemId") String itemId) {
        System.out.println(itemId);
        CommonResponse commonResponse = itemService.updateIsUpByItemId(itemId,0);
        if(commonResponse.isSuccess()){
            return CommonResponse.creatForSuccessMessage("下架成功");
        }else{
            return CommonResponse.creatForError("下架失败");
        }

    }
    @ResponseBody
    @PostMapping("/admin/rebackItem")/*test ok*/
    public CommonResponse rebackItem(@RequestParam("itemId") String itemId) {
        System.out.println("135");
        CommonResponse commonResponse = itemService.updateIsUpByItemId(itemId,1);
        if(commonResponse.isSuccess()){
            return CommonResponse.creatForSuccessMessage("上架成功");
        }else{
            return CommonResponse.creatForError("上架失败");
        }
    }

    @PostMapping("/admin/addCategory")
    public String addCategory( @RequestParam("pic") MultipartFile pic, @RequestParam("name") String name,Model model) {
        String descn = productService.handleFileUpload(pic,name);
        String errorMsg = null;
        if(descn != null) {
            Category category = new Category();
            category.setName(name);
            category.setCatid(name.toUpperCase());
            category.setMainimg(descn);
            CommonResponse<Category> commonResponse = categoryService.addCategory(category);
            if(commonResponse.isSuccess()){
                model.addAttribute("msg","商品种类添加成功！");
                System.out.println("成功");
//                return "seller_item";//商品管理页面
                return "seller_category";
            }else{
                errorMsg = "商品添加失败！";
                System.out.println("123");
                model.addAttribute("errorMsg",errorMsg);
                return "seller_add";//商品添加页面
            }
        }else {
            errorMsg = "商品图片上传失败！";
            model.addAttribute("errorMsg",errorMsg);
            return "seller_add";//商品添加页面
        }



    }

    @PostMapping("/admin/addProduct")/*test ok*/
    public String addProduct(@RequestParam("productid") String productid, @RequestParam("pic") MultipartFile pic, @RequestParam("category") String category, @RequestParam("name") String name,@RequestParam("textdescn") String textdescn, Model model) {
        String descn = productService.handleFileUpload(pic,productid);
        String errorMsg = null;
        if(descn != null) {
            Product product = new Product();
            product.setProductid(productid);
            product.setCategory(category);
            product.setDescn(descn);
            product.setName(name);
            product.setTextdescn(textdescn);
            CommonResponse<Product> commonResponse = productService.addProduct(product);
            if(commonResponse.isSuccess()){
                model.addAttribute("msg","商品添加成功！");
                System.out.println("成功");
                return "seller_product";//商品管理页面
            }else{
                errorMsg = "商品添加失败！";
                System.out.println("123");
                model.addAttribute("errorMsg",errorMsg);
                return "seller_add";//商品添加页面
            }
        }else {
            errorMsg = "商品图片上传失败！";
            model.addAttribute("errorMsg",errorMsg);
            return "seller_add";//商品添加页面
        }

    }
//    @GetMapping("/upload")
//    public String u() {
//        return "test_upload";
//    }

    //注意productid是外键
    @PostMapping("/admin/addItem")/*test ok*/
    public String addItem(@RequestParam("itemId") String itemId,@RequestParam("productId") String productId,@RequestParam("listPrice") String listPrice,@RequestParam("qty") String qty,@RequestParam("attr") String attr,Model model) {
        Item item = new Item();
        item.setItemid(itemId);
        item.setProductid(productId);
        item.setListprice(Double.parseDouble(listPrice));
        item.setQty(Integer.parseInt(qty));
        item.setAttr1(attr);
        CommonResponse<Item> commonResponse = itemService.addItem(item);
        if(commonResponse.isSuccess()){
            model.addAttribute("msg","商品项目添加成功！");
            return "seller_item";//item管理页面
        }else{
            model.addAttribute("errorMsg","商品项目添加失败！");
            return "seller_add";//item添加页面
        }



    }
    @ResponseBody
    @GetMapping ("/admin/itemOrderByPrice")
    public CommonResponse<HashMap<HashMap<String, Object>, Integer>>itemOrderByPrice() {
//        System.out.println(itemService.getItemsOrderedByPrice().getData());
        return itemService.getItemsOrderedByPrice();

    }
    @ResponseBody
    @GetMapping("/admin/itemOrderBySales")
    public CommonResponse<HashMap<HashMap<String, Object>, Integer>> itemOrderBySales() {
        List<HashMap.Entry<String, Integer>> itemListBySale = lineItemService.calculateTotalQuantityAndSort();
        List<String> itemIdList = new ArrayList<>();
        for (HashMap.Entry<String, Integer> entry : itemListBySale) {
            String key = entry.getKey();
            itemIdList.add(key);
        }
        System.out.println("itemIdList"+itemIdList);
//        System.out.println(itemService.getItemListInOrderBySales(itemIdList).getData());
        return itemService.getItemListInOrderBySales(itemIdList);


    }
    @ResponseBody
    @GetMapping("/admin/itemOrderByIsupTrue")
    public CommonResponse<HashMap<HashMap<String, Object>, Integer>> itemOrderByIsupTrue() {
//
//        System.out.println(itemService.getItemListByIsUp(1));
        return itemService.getItemListByIsUp(1);

    }
    @ResponseBody
    @GetMapping("/admin/itemOrderByIsupFalse")
    public CommonResponse<HashMap<HashMap<String, Object>, Integer>> itemOrderByIsupFalse() {
//
//        System.out.println(itemService.getItemListByIsUp(0));
        return itemService.getItemListByIsUp(0);

    }

    @ResponseBody
    @GetMapping("/admin/getItemListByKeyForJS")
    public CommonResponse<HashMap<HashMap<String, Object>, Integer>> getItemListByKeyForJS(String keyword){
        System.out.println("keywords="+keyword);
        System.out.println(itemService.getItemListByKey(keyword).getData());
        return itemService.getItemListByKey(keyword);
    }

}
